package com.west_traveler.ximalayafm;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.west_traveler.application.BaseApplication;
import com.west_traveler.entity.entity_find_second_hot_recommend.SecondHotRecommendAlbum;
import com.west_traveler.entity.entity_find_second_hot_recommend.SecondHotRecommendDetail;
import com.west_traveler.viewpager_adapter.SecondAlbumViewPagerAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SecondRecommendActivity extends AppCompatActivity implements Callback<SecondHotRecommendAlbum> {

    private String id;
    private TextView anchorValue;
    private TextView tv_play_value;
    private TextView tv_state_value;
    private TextView tv_title;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List list = new ArrayList();
    private SecondHotRecommendDetail.DataBean data;
    private String title;
    private String playsCounts;
    private String nickname;
    private String coverMiddle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_recommend);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);

        Intent intent = getIntent();
        id = intent.getStringExtra("ID");
        Log.i("TAG", "接收方ID: " + id);
        title = intent.getStringExtra("Title");
        playsCounts = intent.getStringExtra("PlaysCounts");
        nickname = intent.getStringExtra("Nickname");
        coverMiddle = intent.getStringExtra("CoverMiddle");
        initView();

        //下载数据详情 albumId=3726156&device=android&pageId=null&pageSize=20
        ((BaseApplication) getApplication()).getService().getRecommendDetail(Long.parseLong(id)).enqueue(new Callback<SecondHotRecommendDetail>() {

            @Override
            public void onResponse(Call<SecondHotRecommendDetail> call, Response<SecondHotRecommendDetail> response) {
                data = response.body().getData();
                ((BaseApplication) getApplication()).getService().getRecommendAlbum(Long.parseLong(id), "android", null, 20).enqueue(SecondRecommendActivity.this);
            }
            @Override
            public void onFailure(Call<SecondHotRecommendDetail> call, Throwable t) {
            }
        });
    }

    private void initView() {
        anchorValue = (TextView) findViewById(R.id.tv_anchor_value);
        tv_play_value = (TextView) findViewById(R.id.tv_play_value);
        tv_state_value = (TextView) findViewById(R.id.tv_state_value);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tabLayout = (TabLayout) findViewById(R.id.second_tablayout);
        viewPager = (ViewPager) findViewById(R.id.second_viewpager);
        ImageView imageView = (ImageView) findViewById(R.id.iv_user_icon);
        ((ImageView) findViewById(R.id.iv_tool_back)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        tv_title.setText(title);
        if (Integer.parseInt(playsCounts) > 10000) {
            tv_play_value.setText(Integer.parseInt(playsCounts) / 10000 + "万次");
        } else {
            tv_play_value.setText(playsCounts);
        }
        anchorValue.setText(nickname);
        tv_state_value.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "更新");
        Picasso.with(this).load(coverMiddle).into(imageView);
    }

    @Override
    public void onResponse(Call<SecondHotRecommendAlbum> call, Response<SecondHotRecommendAlbum> response) {
        SecondHotRecommendAlbum.DataBean dataBean = response.body().getData();
        list.add(data);
        list.add(dataBean);
        SecondAlbumViewPagerAdapter adapter = new SecondAlbumViewPagerAdapter(getSupportFragmentManager(), list);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

    }

    @Override
    public void onFailure(Call<SecondHotRecommendAlbum> call, Throwable t) {

    }
}
